import requests

from APIs.baseAPI import BaseAPI
from common.config import ip




class LogAPI(BaseAPI):
    def uploadFile(self):
        file = f'{ip}/file/upload'
        ff = r"E:\dev\python\project\APIjk\datas\abc.jpg"
        # 定义文件参数：'message_img'键来自抓包时的name对应的值，
        # 值是通过数组的形式传值，第一个参数是抓包时的'filename'对应的值，
        #                      第二个参数是通过open打开一个文件，且是二进制读的形式，
        #                      第三个参数是抓包时的文件类型
        fdata = {'file': ('abc.jpg',
                          open(ff, 'rb'),
                          'image/jpeg')}
        filedata = {'type': 'img'}
        rpf = requests.post(url=file, headers=self.header, files=fdata)
        fr = rpf.json()
        self.batchId = fr['batchId']
        return self.batchId


    def upload(self,mydata=None):
        upload = f'{ip}/OaLog/addOrUpdate'

        if mydata is not None:
            self.up = requests.post(url=upload, json=mydata, headers=self.header)
        else:
            self.up = requests.post(url=upload, headers=self.header)

    def upload_result(self):
        return self.up.json()['code']


    def check(self,mydata):
        chkurl = f'{ip}/OaLog/queryList'
        ck = requests.post(chkurl, json=mydata, headers=self.header)
        ck=requests.post(chkurl,headers=self.header)
        # print(ck.json())
        self.ckjson = ck.json()
        logId = self.ckjson['data']['list'][0]['logId']
        # print(logId)
        return logId

    def check_result(self):
        return self.ckjson['code']


    def checkmylog(self):
        chkurl = f'{ip}/OaLog/queryList'
        ckdata = {"page": 1, "limit": 5, "by": "1"}
        ck = requests.post(chkurl, json=ckdata, headers=self.header)
        # print(ck.json())
        ckjson = ck.json()
        self.logId = ckjson['data']['list'][0]['logId']
        return self.logId

    def checkall(self):
        chkurl = f'{ip}/OaLog/queryList'
        ckdata = {"page": 1, "limit": 18952}
        ck = requests.post(chkurl, json=ckdata, headers=self.header)
        # print(ck.json())
        ckjson = ck.json()
        logIds=[]
        ick=ckjson['data']['list']
        for i in ick:
            logIds.append(i['logId'])
        print(logIds)
        return logIds

    def update(self,batchId,logId):
        gxurl = f'{ip}/OaLog/addOrUpdate'
        gxdata = {"logId": logId, "categoryId": "1", "content": "这里是今日内容~~", "tomorrow": "这里明日工作计划~",
                  "question": "这里是今日遇到的问题", "batchId": batchId, "sendUserIds": "3,4", "sendDeptIds": "",
                  "customerIds": "", "contactsIds": "", "businessIds": "", "contractIds": ""}
        self.gx = requests.post(url=gxurl, headers=self.header, json=gxdata)
        # print(gx.json())

    def update_result(self):
        return self.gx.json()['code']

    def delete(self,mydata):
        scurl = f'{ip}/OaLog/deleteById'
        # scdata = {'logId': logId}
        self.sc= requests.post(url=scurl,
                           headers=self.header,
                           data=mydata)
    def delete_result(self):
        return self.sc.json()['code']
if __name__ == '__main__':
    pass